--
--  Copyright 2022 The Open Islands Authors. All Rights Reserved.
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
--
local _M = {
    _VERSION = '0.1'
}

local config = require "config"
local route_table = require "route_table"
local apiserver = require "apiserver"
local constant = require("constant")
local router = require("router")
local log = require("log")

function _M.routing()
    local request_headers = ngx.req.get_headers()
    local dest_node_id = request_headers["dest-node-id"]
    local address, tls_enabled, next_service, err
    if config.node_id ~= "0" and dest_node_id == tostring(config.node_id) then
        -- my node
        address, tls_enabled, err = apiserver.get_node_service_address(constant.WORKER)
        next_service = constant.WORKER
    else
        address, tls_enabled, next_service, err = route_table.get_next_route(dest_node_id)
    end
    router.routing(address, tls_enabled, next_service, err)
end

_M.routing()

